package model;

import java.util.ArrayList;
import java.util.List;

import view.View;
import utilities.*;

public abstract class Model implements Archiveable {
	
	private String name;
	private List<View> views;
	
	public Model(String name) {
		this.name = name;
		views = new ArrayList<View>();
	}
	
	public Model() {}
	
	public String getName() {
		return name;
	}

	@Override
	public String toString()
	{
		return "Model [name=" + name + "]";
	}
	
	public void register( View view ) {
		views.add( view );
	} 
	
	public void unRegister( View view ) {
		views.remove( view );
	}
	
	protected void updateSubscribers() {
		for( View v : views) {
			v.update( this );
		}
	}
	
	public Archive save() {
		Archive archive = ArchiveFactory.createArchive();
		
		archive.putAttribute("name", this.name);
		// save list of views
		
		return archive;
	}
	
	public void load(Archive archive) {
		this.name = archive.getAttribute("name");
		// load views
	}
	
}
